home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / cntrl3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  3.3 KB  |  100 lines

  1. // CNTRL3.CPP - "driver" function for RARS - M. Timin, Dec. 1994
  2. // (see CNTRL0.CPP for better comments)
  3. // adapted to ver. 0.39 3/6/95 by M. Timin
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include "car.h"
  9.  
  10. extern const double CARWID;
  11. extern char* glob_name;
  12.  
  13. con_vec cntrl3(situation s)
  14. {
  15.    const char name[] = "Kernign";
  16.    static int init_flag = 1;
  17.    con_vec result;
  18.    double alpha, vc;
  19.    double width;                      // track width, feet
  20.    double steer_gain = .18;
  21.    const double steer_damp = .48;
  22.    static int lane_time = 0;           // counts time when not in normal lane
  23.    const double normalane = 30.0;
  24.    static double lane = 30;    // determines right-left position on straigtaway
  25.    const double corn_con = 6.5;
  26.    static double corn_spd = 65.0;
  27.  
  28.    if(init_flag)  {
  29.       strcpy(glob_name, name);
  30.       init_flag = 0;
  31.       result.alpha = result.vc = 0;
  32.       return result;
  33.    }
  34.  
  35.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  36.       return result;
  37.  
  38.    if(s.cur_rad == 0.0 && s.nex_rad != 0.0)
  39.       corn_spd = corn_con * sqrt(s.nex_rad > 0.0 ? s.nex_rad : -s.nex_rad);
  40.    else
  41.       corn_spd = corn_con * sqrt(s.cur_rad > 0.0 ? s.cur_rad : -s.cur_rad);
  42.  
  43.    // maybe choose a different lane: (to help in passing)
  44.    if(!s.dead_ahead) {
  45.       if(lane_time <= 0) {
  46.          lane = normalane;
  47.          lane_time = 0;
  48.       }
  49.    }
  50.    else if(!lane_time)   {
  51.       // pick a different lane:
  52.       if(lane >= 80)               // pick a new lane somehow:
  53.          lane = random(60) - 20;
  54.       else if(lane <= -80)
  55.          lane = 20 - random(60);
  56.       else
  57.          lane = random(180) - 90;
  58.       lane_time = 175;              // we will stay in the new lane this long
  59.    }
  60.    if(lane_time > 0)
  61.       --lane_time;
  62.  
  63.    width = s.to_lft + s.to_rgt;                        // find width of track
  64.    if(s.cur_rad == 0) {
  65.       alpha = .25 * steer_gain * (s.to_lft - s.to_rgt - lane) / width;
  66.       if(s.dead_ahead)
  67.           alpha *= 2.0;
  68.     }
  69.    else if(s.cur_rad > 0.0)   {
  70.       alpha = steer_gain * (4 * (s.to_lft -
  71.                          (s.nex_rad < 0.0 ? width/3 : 1.6*CARWID)
  72.                                         ) / width + .2 * width/s.to_rgt);
  73.       if(s.dead_ahead)
  74.          alpha *= .7;
  75.  
  76.    }
  77.    else  {
  78.       alpha = -steer_gain * (4 * (s.to_rgt -
  79.                                (s.nex_rad > 0.0 ? width/3 : 1.6*CARWID)
  80.                                              ) / width  + .2 * width/s.to_lft);
  81.       if(s.dead_ahead)
  82.          alpha *= .7;
  83.    }
  84.    alpha -= steer_damp * s.vn / s.v;  // This is damping, to prevent oscillation
  85.  
  86.    if(s.cur_rad == 0)         // If we are on a straightaway,
  87.       if(s.to_end/s.cur_len > .25)      // if we are far from the end:
  88.          vc = s.v + 85/s.v;        // keep accellerating near full power
  89.       else  {                 // otherwise,
  90.          vc = corn_spd;           // maintain cornering speed, braking at first
  91.          // this next formala will not work if a rt. turn follows the straight:
  92.          alpha += .4 * (s.cur_len/(s.to_end + s.cur_len) - (1/1.25 ));
  93.       }
  94.    else            // if we're in the curve, maintain speed, +
  95.       vc = corn_spd + 1.2 / (s.to_end + .6);
  96.  
  97.    result.vc = vc;   result.alpha = alpha;
  98.    return result;
  99. }                                                              // v;
  100.